package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.BuildingAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.BuildingUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.Building;
import tk.uodrad.apex.project.pojo.vo.BuildingListVO;
import tk.uodrad.apex.project.service.BuildingService;
import tk.uodrad.apex.project.mapper.BuildingMapper;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.utils.InitRandomCode;
import tk.uodrad.apex.project.web.ServiceCode;

/**
* @author Administrator
* @description 针对表【building_management】的数据库操作Service实现
* @createDate 2023-04-04 20:49:37
*/
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building>
    implements BuildingService{
    
    @Autowired
    private BuildingMapper buildingMapper;

    /**
     * 添加单个楼栋
     * @param buildingAddNewDTO 添加楼栋DTO
     */
    @Override
    public void save(BuildingAddNewDTO buildingAddNewDTO) {
        //新建一个Building对象并将传入的DTO复制给Building
        Building building = new Building();
        BeanUtils.copyProperties(buildingAddNewDTO, building);
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<Building> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有name等于当前Building的name
        wrapper.eq("name", building.getName());
        Building selectOne = buildingMapper.selectOne(wrapper);
        if (selectOne != null) {
            String message = "新增数据失败，当前数据已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //生成编号
        building.setCode(InitRandomCode.initCode("bm"));
        //插入数据，并判断是否插入成功
        int rows = buildingMapper.insert(building);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    /**
     * 根据ID修改楼栋
     * @param buildingUpdateDTO 修改楼栋DTO
     */
    @Override
    public void updateById(BuildingUpdateDTO buildingUpdateDTO) {
        //根据传入的DTO中的ID查询数据库中是否存在该数据
        Building buildingById = buildingMapper.selectById(buildingUpdateDTO.getId());
        if (buildingById == null) {
            String message = "修改数据失败，当前数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //创建一个Property对象，将传入的DTO复制给Property对象
        Building building = new Building();
        BeanUtils.copyProperties(buildingUpdateDTO, building);
        //更新数据，并判断是否更新成功
        int rows = buildingMapper.updateById(building);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    /**
     * 分页查询成员列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<BuildingListVO> BuildingListVO分页对象
     */
    @Override
    public IPage<BuildingListVO> list(Integer pageNum, Integer pageSize,String communityCode) {
        Page<BuildingListVO> page = new Page<>(pageNum-1,pageSize);
        return buildingMapper.selectPageVo(page,communityCode);
    }

    @Override
    public Integer countBuilding() {
        return buildingMapper.selectSumBuilding();
    }

    @Override
    public Integer countHouseholds() {
        return buildingMapper.selectAllByHouseholdsInteger();
    }
}




